//
//  HMPayOrderController.m
//  HMMarket
//
//  Created by 孙鹏飞 on 2017/5/9.
//  Copyright © 2017年 homai. All rights reserved.
//

#import "HMPayOrderController.h"
#import "HMApplyForGoodsViewController.h"
#import "OrderCell.h"
#import "OrderFooter.h"
#import "OrderHeader.h"

#import "OrderModel.h"
#import "HMPayOrderViewController.h"
#import "HMReturnGoodViewController.h"
@interface HMPayOrderController () <OrderFooterDelegate>

@property (nonatomic, strong) NSMutableArray * orderArray;
@property (nonatomic, strong) NSMutableArray * detailArray;
@property (nonatomic, strong) UIImageView * nodata;

@end

@implementation HMPayOrderController

- (instancetype)initWithStyle:(UITableViewStyle)style {
    return [super initWithStyle:UITableViewStyleGrouped];
}


- (NSMutableArray *)orderArray
{
    if (_orderArray == nil) {
        _orderArray = [NSMutableArray array];
    }
    return _orderArray;
}

- (NSMutableArray *)detailArray
{
    if (_detailArray == nil) {
        _detailArray = [NSMutableArray array];
    }
    return _detailArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = kBackGolor;
    [self setupTableView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData) name:orderRe object:nil];
}

- (void)refreshData
{
    [self loadNewData];
}
#pragma mark - 初始化刷新控件
- (void)setupRefresh
{
    // header
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    [self.tableView.mj_header beginRefreshing];
    
    // footer
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}

static int currentPage = 1;
/** 下拉刷新 */
- (void)loadNewData
{
    currentPage = 1;
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"uid"] = [MGAccountTool getUid];
    params[@"state"] = @"3";
    params[@"p"] = @(currentPage);
    
    [HttpBaseTool post:kOrderUrl params:params success:^(id responseObj) {
        
        self.orderArray = [OrderModel mj_objectArrayWithKeyValuesArray:responseObj];
        if (self.orderArray.count == 0) {
            _nodata.hidden = NO;
        } else {
            _nodata.hidden = YES;
        }
        [self.tableView reloadData];
        self.tableView.mj_footer.hidden = NO;
        [self.tableView.mj_header endRefreshing];
        
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [HMHUDManager showHUDWithText:NetWorkError];
    }];
}

/** 上拉加载 */
- (void)loadMoreData
{
    NSLog(@"%d",currentPage);
    currentPage++;
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"uid"] = [MGAccountTool getUid];
    params[@"state"] = @"3";
    params[@"p"] = @(currentPage);
    
    [HttpBaseTool post:kOrderUrl params:params success:^(id responseObj) {
        NSArray * moreDataArray = [OrderModel mj_objectArrayWithKeyValuesArray:responseObj];
        [self.orderArray addObjectsFromArray:moreDataArray];
        if (self.orderArray.count == 0) {
            _nodata.hidden = NO;
        } else {
            _nodata.hidden = YES;
        }
        [self.tableView reloadData];
        if (moreDataArray.count == 0) {
            self.tableView.mj_footer.hidden = YES;
        } else {
            [self.tableView.mj_footer endRefreshing];
        }
    } failure:^(NSError *error) {
        currentPage--;
        [self.tableView.mj_footer endRefreshing];
        [HMHUDManager showHUDWithText:NetWorkError];
    }];
}

- (void)setupTableView
{
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.tableView registerNib:[UINib nibWithNibName:@"OrderCell" bundle:nil] forCellReuseIdentifier:@"OrderCell"];
    
    _nodata =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"default_indent"]];
    _nodata.centerX =KWidthSCREEN/2;
    _nodata.centerY =(KHeightSCREEN - 250)/2;
    [self.tableView addSubview:_nodata];
    [self setupRefresh];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    self.tableView.mj_footer.hidden = (self.orderArray.count == 0);
    return self.orderArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    OrderModel * model = self.orderArray[section];
    self.detailArray = model.list;
    return self.detailArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 95;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    OrderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderCell" forIndexPath:indexPath];
    OrderModel * model = self.orderArray[indexPath.section];
    cell.order = model.list[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 49;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    OrderHeader * header = [OrderHeader orderHeader];
    header.model = self.orderArray[section];
    return header;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    OrderFooter * footer = [OrderFooter orderFooter];
    footer.delegate = self;
    footer.model = self.orderArray[section];
    return footer;
}

#pragma mark - footerDelegate
- (void)OrderFooter:(OrderFooter *)footer didClickedGoPayBtn:(OrderModel *)model
{
    DefaultWeakSelf;
    HMPayOrderViewController *VC = [[HMPayOrderViewController alloc] init];
    VC.oid = model.oid;
    VC.orederType = WAIT_PAY;
    VC.post_status = WAIT_PAY_POST;

    VC.refresh = ^void *(NSString *nothing) {
        [weakSelf loadNewData];
        return @"";
    };
    [self.navigationController pushViewController:VC animated:YES];
    MyLog(@"去付款");
}

- (void)OrderFooter:(OrderFooter *)footer didClickedReturnGoodBtn:(OrderModel *)model
{
    MyLog(@"申请退货");
    HMApplyForGoodsViewController *vc = [[HMApplyForGoodsViewController alloc] init];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)OrderFooter:(OrderFooter *)footer didClickedReturnOfBtn:(OrderModel *)model
{
    NSLog(@"退货中");
    HMReturnGoodViewController *VC = [[HMReturnGoodViewController alloc] init];
    VC.model = model;
    [self.navigationController pushViewController:VC animated:YES];
}

- (void)OrderFooter:(OrderFooter *)footer didClickedReturnOkBtn:(OrderModel *)model
{
    NSLog(@"退货完成");
}

@end
